/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2024-01-20 14:06:27
 * @LastEditors: YangJianFei
 * @LastEditTime: 2024-08-15 14:29:25
 * @FilePath: /yt_deveop_app/lib/pages/site/site_observer_list/views/my_top_tabbar.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yt_devops/components/text_width.dart';
import 'package:yt_devops/components/yt_text_style.dart';
import 'package:yt_devops/styles/styles.dart';

import 'my_custom_indicator.dart';

class MyTopTabBar<T> extends StatelessWidget {
  final TabController tabController;
  final List<T> tabs;
  bool isScrollable;
  TabAlignment tabAlignment;
  String tabLabelName;
  // 选中文字样式
  final TextStyle _labelStyle;
  // 未选中文字样式
  final TextStyle _unselectedLabelStyle;

  final Decoration _indicator;

  MyTopTabBar(
    this.tabController,
    this.tabs, {
    this.isScrollable = true,
    this.tabAlignment = TabAlignment.center,
    this.tabLabelName = 'label',
    TextStyle? labelStyle,
    TextStyle? unselectedLabelStyle,
    Decoration? indicator,
    Key? key,
  })  : _labelStyle = labelStyle ??
            ytTextStyle(
              fontSize: 16,
              fontWeight: FontWeight.bold,
              color: Colour.primaryColor,
            ),
        _unselectedLabelStyle = unselectedLabelStyle ??
            ytTextStyle(
              fontSize: 16,
              fontWeight: FontWeight.normal,
              color: Colour.mainTextColor,
            ),
        _indicator = indicator ?? const MyCustomIndicator(),
        super(key: key);

  String getTabLabel(T tab) {
    if (tab is String) {
      return tab;
    } else if (tab is Map) {
      return tab[tabLabelName];
    } else {
      return tab.toString();
    }
  }

  @override
  Widget build(BuildContext context) {
    _getAllTabWidth();
    return Container(
      color: Colors.white,
      child: TabBar(
          isScrollable: isScrollable,
          //是否可以滚动
          labelPadding: const EdgeInsets.only(left: 12, right: 12),
          controller: tabController,
          indicatorSize: TabBarIndicatorSize.tab,
          tabAlignment: tabAlignment,
          //新版本flutter要加上这一句,不然左侧很大间距
          dividerHeight: 0,
          indicator: _indicator,
          labelStyle: _labelStyle,
          unselectedLabelStyle: _unselectedLabelStyle,
          tabs:
              // getTabs()
              // !isScrollable?
              getTabs()
          //   :  tabs.map((e) {
          // return Tab(
          //   child: Container(
          //     width: 100,
          //     color: Colors.red,
          //     alignment: Alignment.center,
          //     child: Text(e,maxLines: 1,),
          //   ),
          // );
          // }).toList(),
          ),
    );
  }

  List<Widget> getTabs() {
    List<Widget> list = [];
    for (var element in tabs) {
      var width = TextWidth.calculateTextSize(
              getTabLabel(element),
              ytTextStyle(
                fontSize: 16,
                fontWeight: FontWeight.bold,
                color: Colour.primaryColor,
              )) +
          16.w;
      var con = Container(
        height: 40,
        color: Colors.white,
        alignment: Alignment.center,
        width: width,
        child: Text(
          getTabLabel(element),
          maxLines: 2,
        ),
      );
      list.add(con);
    }
    return list;
  }

  ///获取所有tab的宽度 如果小于屏幕宽度 则不能滚动,撑开tab
  _getAllTabWidth() {
    double? allTabWidth = 0;
    List<double> widthList = [];
    for (var element in tabs) {
      var width =
          TextWidth.calculateTextSize(getTabLabel(element), _labelStyle) + 48.w;
      widthList.add(width);
    }
    if (widthList.isNotEmpty) {
      allTabWidth = widthList.reduce(max) * tabs.length;
    }
    // Log.d('allTabWidth====' + allTabWidth.toString());
    // Log.d('screenWidth====' + ScreenUtil().screenWidth.toString());
    if (allTabWidth <= ScreenUtil().screenWidth) {
      isScrollable = false;
      tabAlignment = TabAlignment.fill;
    } else {
      isScrollable = true;
      tabAlignment = TabAlignment.center;
    }
  }
}
